package com.flca.frw.components
{
	import mx.containers.VBox;
	import com.flca.frw.interfaces.ISimpleCtlr;
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	import flash.display.DisplayObject;
	import mx.core.IFlexDisplayObject;
	import flash.utils.getDefinitionByName;
	import com.flca.frw.util.GuiUtil;
	import com.flca.frw.interfaces.IModel;
	import mx.rpc.events.ResultEvent;
	import mx.controls.Alert;
	import mx.core.Application;
	
	public class AssociationOneCtrl extends VBox
	{
		public const ASS_LOOKUP:int 	= 0;
		public const ASS_DETFORM:int = 1;
		public const ASS_MIXED:int 		= 2;
		
		[Bindable]
		public var assValue : Object;//this will be used to fill the TextInput
		
		/*
		 * Classname of xxxModel to control label and colums
		 */ 
		public var assModel : IModel; 
		
		/*
			the detailform to display when modus is BELONG_TO or MIXED
		*/
		public var assDetailForm : DisplayObject; 
		

		/*
		use one the const above. This correspons with assDetailForm
		With LOOKUP you probably use the LookupForm
		With BELONGS_TO you probably use a XxxDetailForm (or similar)
		With MIXED you probably use a XxxMaintForm (or similar)
		*/
		public var assModus : int;
				
		/*
		this is used to center the popup
		*/
		public var assParentWindow:DisplayObject;

		/*
		convenience method to pass all params for modus = LOOKUP
		*/		
		public function setupForLookup(value: Object, formatModel :IModel, parentWindow: DisplayObject) : void 
		{
			assModus = ASS_LOOKUP;
			assValue = value;
			assModel = formatModel;
			assParentWindow = parentWindow;
			init();
		}

		/*
		convenience method to pass all params for modus = LOOKUP
		*/		
		public function setupForDetailForm(value: Object, 
			detailForm:DisplayObject, formatModel :IModel, parentWindow: DisplayObject) : void 
		{
			assModus = ASS_DETFORM;
			assValue = value;
			assDetailForm = detailForm;
			assModel = formatModel;
			assParentWindow = parent;
			init();
		}

		//--- private vars
		private var popupForm : DetailForm;
		private var selectForm : LookupForm;
		
		private var objectGetter : Function;
		
		protected function onBtnSelectClicked() : void {
			if (assModus == ASS_LOOKUP) {
				onLookupClicked();	
			} else {
				assModel.makeNew();
				onAddOrEditClicked(assModel.getEntity);
			}
		}

		public function init() : void {
			if (assValue != null) {
				if (assModel != null) {
					view.idValue.text = assModel.getLabel(assValue);
				} else {
					view.idValue.text = assValue.toString()
				}
			}
		}


		private function onAddOrEditClicked(aGetter:Function) : void 
		{
			objectGetter = aGetter;
			popupForm = PopUpManager.createPopUp(parentWnd, DetailForm, true) as DetailForm;
			popupForm.detailView = assDetailForm;
			popupForm.onCancelClicked = onDetailFormCancelClicked;
			popupForm.onSaveClicked = onDetailFormSaveClicked;
			popupForm.setModelGetter(objectGetter);
			PopUpManager.centerPopUp(popupForm);
			popupForm.y = popupForm.y + 100;
			popupForm.init();
			popupForm.updateViewFromModel();
		}

		private function onLookupClicked() : void 
		{
			selectForm = PopUpManager.createPopUp(parentWnd, LookupForm, true) as LookupForm;
			selectForm.onCancelClicked = onLookupFormCancelClicked;
			selectForm.onSelectClicked = onLookupFormSelectClicked;
			selectForm.gridFormatter = assModel;
			PopUpManager.centerPopUp(selectForm);
			if (assModel.getLookupValues != null) {
				selectForm.lookupValues = assModel.getLookupValues();
				selectForm.init();
			} else {
				Alert.show("assModel.getLookupValues is null");
			}
		}

		
		private function onLookupFormSelectClicked() : void {
			view.idValue.text = selectForm.idLookupValues.selectedItem.toString();
			assValue = selectForm.idLookupValues.selectedItem;
			PopUpManager.removePopUp(selectForm);
		}
		
		private function onLookupFormCancelClicked() : void {
			PopUpManager.removePopUp(selectForm);
		}
		

		private function onDetailFormSaveClicked() : void {
			if (popupForm.validate()) {
				popupForm.updateModelFromView();
				PopUpManager.removePopUp(popupForm);
			}
		}
		
		private function onDetailFormCancelClicked() : void {
			PopUpManager.removePopUp(popupForm);
		}
		
		internal function get view() : AssociationOneForm {
			return this as AssociationOneForm;
		}

		protected function trl(s:String) : String {
			return s;	
		}
		
		protected function  get parentWnd() : DisplayObject {
			if (assParentWindow != null) {
				return assParentWindow;
			} else {
				return this;//TODO
			}
		}
	}
}